﻿using eslib.FileManageLib;
using eslib.tools.config;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ArticleClientLib
{
    /// <summary>
    /// 本地软文
    /// </summary>
    public class LocalArticle
    {
        /// <summary>
        /// 软文目录路径，整个目录是一个软文
        /// </summary>
        public string Dir { get; set; }


        /// <summary>
        /// 软文id，未上传时为null
        /// </summary>
        public long? ArticleId { get; set; }

        /// <summary>
        /// 通过软文目录名获取id
        /// </summary>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public long GetArticleIdByDirName()
        {
            try
            {
                string d = Path.GetFileName(Dir);
                return long.Parse(d);
            }
            catch { throw new Exception("软文目录名不正确"); }
        }


        /// <summary>
        /// 以本地目录名（同步目录下）获取ArtcileId
        /// </summary>
        /// <returns></returns>
        public long GetDirArticleId()
        {
            string dir = Path.GetFileName(Dir);
            if (long.TryParse(dir, out long articleId))
            {
                return articleId;
            }
            else throw new Exception("软文目录名不正确，只能使用同步目录下的软文获取软文Id");
        }


        public LocalArticle(string dir)
        {
            Dir = dir;
        }



        string getPath(string file)
        {
            return Path.Combine(Dir, file);
        }


        /// <summary>
        /// 检测数据完整性
        /// </summary>
        /// <returns></returns>
        public (bool pass, string log) Check()
        {
            bool pass = true;
            string log = "";
            try
            {
                var item = Article;
            }
            catch
            {
                pass = false;
                log += ($"软文数据异常,{Dir}\r\n");
            }

            try
            {
                var item = mainPng;
            }
            catch
            {
                pass = false;
                log += ($"主图异常,{Dir}\r\n");
            }

            try
            {

                var img = Images;
            }
            catch (Exception e)
            {
                pass = false;
                log += ($"图片异常,{Dir},{e.Message}\r\n");
            }

            return (pass, log);
        }


        /// <summary>
        /// 软文（动态读取）
        /// </summary>
        public ArticleJson Article
            => GenericJsonConfig.Load<ArticleJson>(getPath("doc.json"));


        /// <summary>
        /// 主图（动态读取）
        /// </summary>
        public ArticleImageJson mainPng
            => ArticleImageJson.OpenImage(getPath("main.png"));


        /// <summary>
        /// 图片集（动态读取）
        /// </summary>
        public List<ArticleImageJson> Images
        {
            get
            {
                List<ArticleImageJson> list = new();

                List<string> imgs = new();
                string[] exts = new string[] { "*.jpg", "*.png" };
                foreach (var ext in exts)
                {
                    imgs.AddRange(Directory.GetFiles(getPath("images"), ext));
                }

                foreach (var img in imgs)
                {
                    list.Add(ArticleImageJson.OpenImage(img));
                }

                return list;
            }
        }


        /// <summary>
        /// 删除(慎重）
        /// </summary>
        public void Delete()
        {
            Dir d = new Dir(this.Dir);
            d.LoadChildren();
            d.Delete();
        }
    }



}
